package cz.muni.fi.rum.gui;

import cz.muni.fi.rum.RumConfigurator;
import cz.muni.fi.rum.domainmodel.Reflector;
import cz.muni.fi.rum.gui.component.ConnectionPane;
import cz.muni.fi.rum.gui.component.HistoryPane;
import cz.muni.fi.rum.gui.component.InOutMonitorPane;
import cz.muni.fi.rum.gui.component.ModelPane;
import cz.muni.fi.rum.gui.component.SaveAble;
import cz.muni.fi.rum.gui.component.ServerManagerPane;
import cz.muni.fi.rum.gui.component.StartInfoPane;
import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JOptionPane;
import org.noos.xing.mydoggy.Content;
import org.noos.xing.mydoggy.ContentManager;
import org.noos.xing.mydoggy.ContentManagerUIListener;
import org.noos.xing.mydoggy.FloatingTypeDescriptor;
import org.noos.xing.mydoggy.MultiSplitContentManagerUI;
import org.noos.xing.mydoggy.TabbedContentManagerUI;
import org.noos.xing.mydoggy.TabbedContentUI;
import org.noos.xing.mydoggy.ToolWindow;
import org.noos.xing.mydoggy.ToolWindowAnchor;
import org.noos.xing.mydoggy.ToolWindowTab;
import org.noos.xing.mydoggy.ToolWindowType;
import org.noos.xing.mydoggy.event.ContentManagerUIEvent;
import org.noos.xing.mydoggy.plaf.MyDoggyToolWindowManager;
import org.noos.xing.mydoggy.plaf.ui.content.MyDoggyMultiSplitContentManagerUI;

/**
 * Trida pro ovladani celeho dockovaciho prostredi
 * @author pmikulasek
 */
public class MyDoggyController {

    public static final String MODEL_ID = "Model";
    public static final String HISTORY_ID = "History";
    public static final String MONITOR_ID = "Monitor";
    public static final String CONNECTOR_ID = "Connector";
    public static final String SERVER_ID = "Servers";
    private static MyDoggyController instance;
    private MyDoggyToolWindowManager wm;

    private MyDoggyController() {
        registerAllToolWindows();
        setDefaultConfigurationForToolWindow();
        initBaseContent();
    }

    private void registerAllToolWindows() {
        MyDoggyToolWindowManager myDoggyToolWindowManager = new MyDoggyToolWindowManager();
        this.wm = myDoggyToolWindowManager;
        ModelPane.getInstance().setWindowManager(wm);
        wm.registerToolWindow(MODEL_ID, "Reflector Model", null, ModelPane.getInstance(), ToolWindowAnchor.LEFT);
        wm.registerToolWindow(HISTORY_ID, "History", null, HistoryPane.getInstance(), ToolWindowAnchor.LEFT);
        wm.registerToolWindow(MONITOR_ID, "In Out Comunication", null, InOutMonitorPane.getInstance(), ToolWindowAnchor.BOTTOM);
        ToolWindow connectorToolWindow = wm.registerToolWindow(CONNECTOR_ID, "Connect", null, ConnectionPane.getInstance(), ToolWindowAnchor.LEFT);
        wm.registerToolWindow(SERVER_ID, "Reflector Servers", null, ServerManagerPane.getInstance(), ToolWindowAnchor.LEFT);
        for (ToolWindow window : wm.getToolWindows()) {
            window.setAvailable(false);
        }
        connectorToolWindow.addPropertyChangeListener("visible", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (!(Boolean) evt.getNewValue()) {
                    ToolWindow tool = (ToolWindow) evt.getSource();
                    tool.setAvailable(false);
                }
            }
        });
    }

    public static MyDoggyController getInstance() {
        if (instance == null) {
            instance = new MyDoggyController();
        }
        return instance;
    }

    public MyDoggyToolWindowManager getWindowManager() {
        return wm;
    }

    public void setDefaultConfigurationForToolWindow(/*ToolId id*/) {
        //nastaveni connector window a servers tab
        ToolWindow connector = wm.getToolWindow(CONNECTOR_ID);
        ToolWindow servers = wm.getToolWindow(SERVER_ID);

        ToolWindowTab serversTab = connector.addToolWindowTab(servers);
        serversTab.setCloseable(false);
//        DockedTypeDescriptor dockedTypeDescriptor = (DockedTypeDescriptor) connector.getTypeDescriptor(ToolWindowType.DOCKED);
//        dockedTypeDescriptor.setEnabled(false);
//        SlidingTypeDescriptor slidingTypeDescriptor = (SlidingTypeDescriptor) connector.getTypeDescriptor(ToolWindowType.SLIDING);
//        slidingTypeDescriptor.setEnabled(false);

        FloatingTypeDescriptor floatingTypeDescriptor = (FloatingTypeDescriptor) connector.getTypeDescriptor(ToolWindowType.FLOATING);
        floatingTypeDescriptor.setEnabled(true);
        floatingTypeDescriptor.setLocation(150, 200);
        floatingTypeDescriptor.setSize(320, 200);
        floatingTypeDescriptor.setModal(false);
        floatingTypeDescriptor.setTransparentMode(true);
        floatingTypeDescriptor.setTransparentRatio(0.5f);
        floatingTypeDescriptor.setTransparentDelay(1000);
        floatingTypeDescriptor.setAnimating(true);
//        connector.setType(ToolWindowType.FLOATING_LIVE);
//        floatingTypeDescriptor.setAutoHide(true);
        //nastaveni history window
//        ToolWindow history = wm.getToolWindow(HISTORY_ID);
//        history.setType(ToolWindowType.DOCKED);
//        //nastaveni monitor window
//        ToolWindow monitor = wm.getToolWindow(MONITOR_ID);
//        monitor.setType(ToolWindowType.DOCKED);


    }

    public void showToolWindow(String toolId) {
        ToolWindow toolWindow = wm.getToolWindow(toolId);
        toolWindow.setAvailable(true);
        toolWindow.setActive(true);
        toolWindow.setVisible(true);
    }

    public void initBaseContent() {
        ContentManager contentManager = wm.getContentManager();
        Content content = contentManager.addContent("Start", "Start", null, new StartInfoPane());
        content.setToolTipText("Start tip");
        MultiSplitContentManagerUI contentManagerUI = new MyDoggyMultiSplitContentManagerUI();
        contentManager.setContentManagerUI(contentManagerUI);

        contentManagerUI.setShowAlwaysTab(false);
        contentManagerUI.setTabPlacement(TabbedContentManagerUI.TabPlacement.TOP);
        contentManagerUI.setTabLayout(TabbedContentManagerUI.TabLayout.SCROLL);
        contentManagerUI.setMinimizable(false);

        contentManagerUI.addContentManagerUIListener(new ContentManagerUIListener() {

            @Override
            public boolean contentUIRemoving(ContentManagerUIEvent event) {
                SaveAble component = (SaveAble) event.getContentUI().getContent().getComponent();
                if (!component.isSaved()) {
                    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getResourceMap(MainFrame.class);

                    int confirm = JOptionPane.showConfirmDialog(RumConfigurator.getApplication().getMainFrame(), resourceMap.getString("issaved.text"));
                    if (confirm
                            == JOptionPane.OK_OPTION) {
                        component.saveAction();
                        return component.isSaved();
                    } else if (confirm == JOptionPane.NO_OPTION) {
                        return true;
                    }
                    return false;
                } else {
                    return true;
                }
            }

            public void contentUIDetached(ContentManagerUIEvent event) {
//                JOptionPane.showMessageDialog(RumConfigurator.getApplication().getMainFrame(), "Hello World!!!");
            }
        });
        TabbedContentUI contentUI = contentManagerUI.getContentUI(wm.getContentManager().getContent(0));

        contentUI.setCloseable(false);
        contentUI.setDetachable(true);
        contentUI.setTransparentMode(true);
        contentUI.setTransparentRatio(0.2f);
        contentUI.setTransparentDelay(500);
        contentManager.setEnabled(false);
//        contentManager.addContent("Tree Key 2", "Tree Title 2", null, new JTree(), null,
//                new MultiSplitConstraint(contentManager.getContent(0), 0));


    }

    public void showBaseDesktop(Reflector reflector) {
        ToolWindow connector = wm.getToolWindow(CONNECTOR_ID);
        connector.setAvailable(false);
        ToolWindow history = wm.getToolWindow(HISTORY_ID);
        history.setAvailable(true);
        history.setVisible(true);
        ToolWindow model = wm.getToolWindow(MODEL_ID);
        Component component = model.getComponent();
        ModelPane modelPane = (ModelPane) component;
        modelPane.setReflector(reflector);
        model.setAvailable(true);
        model.setVisible(true);
        ToolWindow monitor = wm.getToolWindow(MONITOR_ID);
        monitor.setAvailable(true);
        monitor.setVisible(true);
        ContentManager contentManager = wm.getContentManager();
        contentManager.setEnabled(true);


    }

    public void showBaseDesktop() {
        ToolWindow connector = wm.getToolWindow(CONNECTOR_ID);
        connector.setAvailable(false);
        ToolWindow model = wm.getToolWindow(MODEL_ID);
        model.setAvailable(true);
        model.setVisible(true);
        model.setActive(true);
        ToolWindow history = wm.getToolWindow(HISTORY_ID);
        history.setAvailable(true);
        history.setVisible(true);
        ToolWindow monitor = wm.getToolWindow(MONITOR_ID);
        monitor.setAvailable(true);
        monitor.setVisible(true);
        ContentManager contentManager = wm.getContentManager();
        contentManager.setEnabled(true);

    }
}
